<?php

class User_Action_Signup extends Action
{
	function execute($request)
	{
		$form = Form::factory('user/signup');

		if ($request -> isPost() && $form -> isValid($_POST))
		{
			$table = Model::getTable('user');
			$user = $table -> fetchNew();

			$data = $form -> getData();

			$email = $data['email'];

			$user -> email = $data['email'];
			$user -> name = $data['name'];

			$user -> created_at = time();
			$user -> created_ip = $request -> getClientIp();

			$user -> is_verified = 1;
			$user -> is_approved = 1;
			$user -> is_active = 1;
			$user -> level_id = 5;

			$user -> setPassword($data['password']);

			$user -> save($safe = TRUE);

			$table2 = Model::getTable('user/verification');

			$verify = $table2 -> fetchNew();

			$verify -> verification_id = $code = Text::random(32);

			$verify -> type = 'user/signup';

			$verify -> created_at = time();

			$verify -> user_id = $user -> _id;

			$verify -> save();

			/*
			 * $mailer = Mailer::inst() -> addReceipient($data['email'],
			 * $data['displayname']) -> setSubject('email kích hoạt tài khoản') ->
			 * setMessage('mail/user/verification.tpl', $data = array(
			 'displayname' => $data['displayname'],
			 'email' => $data['email'],
			 'password' => $password,
			 'url' => Agilet::getUrl('user/verify/?code=' . $code),
			 )) -> send();
			 */

			Response::redirect('user/signup/success');
		}

		$request -> response = View::render('user/html/signup', $data = array('form' => $form));
	}

}
